package edu.jmu.springmvc;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;



@Controller
@RequestMapping("/user")
public class UserController {
	
	@RequestMapping("/handle82")
	public @ResponseBody String handle82(User user){
		System.out.println("UserController.handle82():"+user);
		return user.toString();
	}
	
	@RequestMapping(value="/register",method=RequestMethod.GET)
	public String register(User1 user1){
		System.out.println("进入注册页面!");
		return "UserRegister";
	}
	
	@RequestMapping(value="/register",method=RequestMethod.POST)
	public String doRegister(User1 user1){
		System.out.println("正在保存用户!");
		System.out.println(user1);
		return "user1";
	}
	
	@RequestMapping(value="/showUserListByXlsx")
	public String showUserListExcel(ModelMap mm)
	{
		List<User1> user1List = new ArrayList<User1>();
		User1 user1 = new User1();user1.setFirstName("张");user1.setLastName("三");
		User1 user2 = new User1();user2.setFirstName("李");user2.setLastName("四");
		user1List.add(user1);
		user1List.add(user2);
		mm.addAttribute(user1List);
		return "userListExcel";
	}
	
	@RequestMapping(value="/showUserListByPdf")
	public String showUserListInPdf(ModelMap mm){
		List<User1> user1List = new ArrayList<User1>();
		User1 user1 = new User1();user1.setFirstName("Zhang");user1.setLastName("三");
		User1 user2 = new User1();user2.setFirstName("Li");user2.setLastName("四");
		user1List.add(user1);
		user1List.add(user2);
		mm.addAttribute(user1List);
		return "userListPdf";
	}
	
	@RequestMapping(value="/showUserListByXml")
	public String showUserListInXml(ModelMap mm){
		List<User1> user1List = new ArrayList<User1>();
		User1 user1 = new User1();user1.setFirstName("Zhang");user1.setLastName("三");
		User1 user2 = new User1();user2.setFirstName("Li");user2.setLastName("四");
		user1List.add(user1);
		user1List.add(user2);
		List<User> userList = new ArrayList<User>();
		User user = new User();user.setAge(10);user.setUserName("郑成功");
		userList.add(user);
		mm.addAttribute(userList);
		mm.addAttribute(user1List);
		return "userListXml";
	}
	
	@RequestMapping(value="/showUserListByJson")
	public String showUserListByJson(ModelMap mm) {
		List<User1> user1List = new ArrayList<User1>();
		User1 user1 = new User1();user1.setFirstName("张");user1.setLastName("三");
		User1 user2 = new User1();user2.setFirstName("李");user2.setLastName("四");
		user1List.add(user1);
		user1List.add(user2);
		mm.addAttribute(user1List);
		return "userListJson";
	}
	@RequestMapping(value="/showUserList2ByJson")
	public String showUserList2ByJson(ModelMap mm) {
		List<User1> user1List = new ArrayList<User1>();
		User1 user1 = new User1();user1.setFirstName("张");user1.setLastName("三");
		User1 user2 = new User1();user2.setFirstName("李");user2.setLastName("四");
		user1List.add(user1);
		user1List.add(user2);
		mm.addAttribute(user1List);
		return "userListJson";
	}
	
	@RequestMapping(value="/showUserListMix")
	public String showUserListMix(ModelMap mm){
		List<User1> user1List = new ArrayList<User1>();
		User1 user1 = new User1();user1.setFirstName("张");user1.setLastName("三");
		User1 user2 = new User1();user2.setFirstName("李");user2.setLastName("四");
		user1List.add(user1);
		user1List.add(user2);
		mm.addAttribute(user1List);
		return "userListMix";
	}
	
	@RequestMapping(value="/throwException")
	public String throwException(){
		if (2>1) {
			throw new RuntimeException();
		}
		
		return "success";
	}
	@ExceptionHandler(RuntimeException.class)
	public String handleException(RuntimeException re, HttpServletRequest request) {
		return "forward:/error.jsp";//转发到web应用根目录下的error.jsp
		//return "error";  //转发到jsps目录下的error.jsp
		
	}
	
	@RequestMapping(value="/testjson")
	public @ResponseBody JavaBean json(){
		return new JavaBean();
		
	}
	
}
